﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCE.UI
{
    public partial class ZMCE50 : EFViewCmes
    {
        private EFTreeItem item;    //记录选中的树节点

        public ZMCE50()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ZMCE50_Loaded);
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZMCE50_Loaded(object sender, RoutedEventArgs e)
        {
            this.systemModuleTree.Refresh();
        }

        /// <summary>
        /// 第一次进入页面时选中树的根节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void systemModuleTree_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)
        {
            if (e.PreparedItem == null)
            {
                return;
            }
            EFTreeItem treeItem = e.PreparedItem as EFTreeItem;
            if (treeItem.Level == 0)
            {
                this.systemModuleTree.SelectedItem = treeItem;//选中
                treeItem.ExpandItem();//展开
            }
        }

        /// <summary>
        /// 选中树节点，导航右侧的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void systemModuleTree_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            item = this.systemModuleTree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = getTreeNodeInfo(item);
            this.container.LoadView<EFCustomViewContainerPanel>("ZMCE51", args);
            //if (item.IsLeaf == false)
            //{
            //    this.container.LoadView<EFCustomViewContainerPanel>("ZMCE51", args);
            //}
            //else
            //{
            //    this.container.LoadView<EFCustomViewContainerPanel>("ZMCS03", args);
            //}
        }

        
        /// <summary>
        /// //获取tree选中的结点对应的label
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        private Dictionary<string, string> getTreeNodeInfo(EFTreeItem item)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            args.Add("fdToencode", item.ItemObject.GetType().GetProperty("fdToencode").GetValue(item.ItemObject, null) as string);
            
            return args;
        }

        /// <summary>
        /// 重新基类的发布数据方法
        /// 刷新树节点
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(Platform.Core.EIInfo data)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            string _value = "";
            if (item != null)
            {
                if (item.IsLeaf)
                {
                    _value = (item.Parent as EFTreeItem).ItemObject.GetType().GetProperty("label").GetValue((item.Parent as EFTreeItem).ItemObject, null) as string;
                    args.Add("node", _value);
                }
                else
                {
                    _value = item.ItemObject.GetType().GetProperty("label").GetValue(item.ItemObject, null) as string;
                    args.Add("node", _value);
                }
            }
            BaseTools.RefreshTree("ZMCE50", "query", args, OnDoWorkCompleted);
        }

        /// <summary>
        /// OnEIDataPublished回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            this.systemModuleTree.EIDataContext = new Platform.Core.UI.EIDataContext();
            this.systemModuleTree.EIDataContext.Update(e.EIInfo);
            item.IsLoadOnDemandEnabled = true;
            if (item.IsLeaf)
            {
                EFTreeItem parent = item.Parent as EFTreeItem;
                string node = item.ItemObject.GetType().GetProperty("label").GetValue(item.ItemObject, null) as string;
                parent.Items.Clear();
                parent.CollapseItem();
                parent.ExpandItem();
                item = BaseTools.FindTreeItem(parent.Items, node, "label") as EFTreeItem;
                this.systemModuleTree.SelectedItem = item;
                return;
            }
            item.Items.Clear();
            item.CollapseItem();
            item.ExpandItem();
        }

    }
}
